<?php
class PlayerController extends Controller
{
	public function filters()
	{
		return array(
			'accessControl',
		);
	}

	public function accessRules()
	{
		return array(
			array('allow',
				'actions' => array('error'),
				'users' => array('*'),
			),		
			array('allow',
				'actions' => array(
					'index',
					'linktracks',
					'playlistsync',
					'saveuser',
					'scrobble',
					'feedback',
					'getfeedbacks',
					'download'
				),
				'users' => array('@'),
			),
			array('deny',
				'users' => array('*'),
			),
		);
	}

	public function actionDownload()
	{
		$request = 'http://poiskm.ru/?q='.$_REQUEST['query'].'&c=search&xbds=1';
		$request = preg_replace('~\s+~', '+', $request);
		$data = Yii::app()->CURL->run($request);
		$data = preg_replace('~src="\/i~', 'src="http://poiskm.ru/i', $data);

		$bad_urls = array(
			'https://apis.google.com/js/plusone.js',
			'http://counter.rambler.ru/top100.jcn\?2502516'
		);

		$data = preg_replace('~'.join($bad_urls,'|').'~', '#', $data);
		echo $data;
	}
	
	public function actionIndex()
	{
		if (!Yii::app()->request->isAjaxRequest) {
			throw new CHttpException(404);
		}
	}

	public function actionError()
	{
		if ($error=Yii::app()->errorHandler->error) {
			if(Yii::app()->request->isAjaxRequest) {
				echo $error['message'];
			} else {
				$this->render('error', $error);
			}
		}
	}
	
	public function actionLinkTracks()
	{
		if (!Yii::app()->request->isAjaxRequest) {
			throw new CHttpException(404);
		}

		$data = array();
		$tracks = $_POST['tracks'];
		
		if (!empty($tracks)) {
			if (count($tracks) >= 2) {
				$data['imhonet_result'] = Yii::app()->Imhonet->run(
					'player',
					array(
						'func' => 'link_tracks',
						'tracks' => $tracks
					)
				);
				$data['tracks'] = $tracks;
			}
		}
		echo json_encode($data);	
	}
	
	public function actionPlaylistSync()
	{
		if (!Yii::app()->request->isAjaxRequest) {
			throw new CHttpException(404);
		}

		$data = array();
		$hashes = $_POST['hashes'];
		$uid = $_POST['owner'];
		
		if ($uid && !empty($hashes)) {
			$imhonet_result = Yii::app()->Imhonet->run(
				'player',
				array(
					'func' => 'playlist_sync',
					'uid' => $uid,
					'hashes' => $hashes
				)
			);

			if (!empty($imhonet_result)) {
				$data['success'] = true;
				$data['unknown_user'] = $imhonet_result->unknown_user;
				$data['unknown_hashes'] = $imhonet_result->unknown_hashes;
			}
		}
		echo json_encode($data);	
	}

	public function actionSaveUser()
	{
		if (!Yii::app()->request->isAjaxRequest) {
			throw new CHttpException(404);
		}

		$params['profiles'] = $_POST['profiles'];
		Yii::app()->Imhonet->run('register', $params);
		
		$data = array(
			'redirect' => '/login'
		);
			
		echo json_encode($data);	
	}

	public function actionScrobble()
	{
		if (!Yii::app()->request->isAjaxRequest) {
			throw new CHttpException(404);
		}

		$hash = $_POST['hash'];
		$imhonet_result = Yii::app()->Imhonet->run(
			'player',
			array(
				'func' => 'scrobble',
				'audio_hash' => $hash
			)
		);
		
		$data = array(
			'success' => true,
			'imhonet_result' => $imhonet_result
		);
		echo json_encode($data);
	}
	
	public function actionFeedback()
	{
		if (!Yii::app()->request->isAjaxRequest) {
			throw new CHttpException(404);
		}

		$hash = $_POST['hash'];
		$attitude = $_POST['attitude'];
		$imhonet_result = Yii::app()->Imhonet->run(
			'player',
			array(
				'func' => 'feedback',
				'audio_hash' => $hash,
				'attitude' => $attitude
			)
		);
		
		$data = array(
			'success' => true,
			'imhonet_result' => $imhonet_result
		);
		echo json_encode($data);
	}

	public function actionGetFeedbacks()
	{
		if (!Yii::app()->request->isAjaxRequest) {
			throw new CHttpException(404);
		}

		$hashes = $_POST['hashes'];
		$imhonet_result = Yii::app()->Imhonet->run(
			'player',
			array(
				'func' => 'get_feedbacks',
				'audio_hashes' => $hashes
			)
		);
		
		$data = array(
			'success' => !empty($imhonet_result->feedbacks),
			'feedbacks' => $imhonet_result->feedbacks
		);
		echo json_encode($data);
	}
}
